package cc.loac.heba.service;

import cc.loac.heba.dao.LoginTokenDao;
import cc.loac.heba.pojo.LoginToken;
import cc.loac.heba.pojo.Result;
import cc.loac.heba.tool.Common;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class LoginTokenService {

    @Resource
    LoginTokenDao loginTokenDao;

    /**
     * 生成 LoginToken
     * @param email Token 对应的邮箱
     * @return Token
     */
    public String createLoginToken(String email) {
        // 生成 LoginToken
        String token = Common.getRandomString(48, false);
        try {
            Date currentTime = new Date();
            Date expiredTime = new Date(currentTime.getTime() + 43200000);
            // 先删除之前的所有 token
            removeLoginTokenByEmail(email);

            int result = loginTokenDao.insertLoginToken(email, token, currentTime, expiredTime);
            if (result > 0) {
                return token;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 根据邮箱删除所有 LoginToken
     * @param email 邮箱
     * @return 改变条数
     */
    public int removeLoginTokenByEmail(String email) {
        int result = 0;
        try {
            result = loginTokenDao.deleteLoginTokenByEmail(email);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 根据 id 数组来删除 LoginToken
     * @param ids id 数组
     * @return 改动条数
     */
    public int removeLoginTokenByIds(List<Integer> ids) {
        int result = 0;
        try {
            result = loginTokenDao.deleteLoginTokenByIds(ids);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 获取所有 LoginToken
     * @return LoginToken 数组
     */
    public List<LoginToken> getAllLoginToken() {
        List<LoginToken> list = null;
        try {
            list = loginTokenDao.selectAllLoginToken();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public LoginToken getLoginTokenByEmailAndToken(String email, String token) {
        LoginToken loginToken = null;
        try {
            loginToken = loginTokenDao.selectLoginTokenByEmailAndToken(email, token);
            return loginToken;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 检查 LoginToken 是否过期或者不存在
     * @param token LoginToken
     * @return Result 实体类
     */
    public Result checkToken(String email, String token) {
        Result result = new Result();
        try {
            LoginToken loginToken = getLoginTokenByEmailAndToken(email, token);
            if (loginToken == null) {
                return Common.tokenIsNull();
            }

            Date currentTime = new Date();
            if (currentTime.getTime() >= loginToken.getExpirationTime().getTime()) {
                result.setStatus(-2);
                result.setMessage("Token不存在或已过期");
                // Token 已经过期，删除 Token
                removeLoginTokenByEmail(email);
                return result;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Common.exceptionResult();
        }
    }

    /**
     * 检查 LoginToken 是否过期或者不存在
     * @param token LoginToken
     * @return Result 格式化 JSON
     */
    public String checkTokens(String email, String token) {
        return Common.toJson(checkToken(email, token));
    }
}
